// pkgArticle/pages/detail/detail.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    recommend: [],
    collect: 'like-o',
    like: 'thumb-circle-o',
    share: 'share-o',
    id:''
  },
  onShareAppMessage () {
    return {
      title: '文章详情',
      path: '/pages/detail/detail?id=' + this.data.id,
      // imageUrl: 

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    this.setData({id:options.id})
    this.getdetails(options.id)
  },
  async getdetails (id) {
    const res = await wx.http.get('/index/show?id=' + id)
    console.log(res);
    if (res.errno === 0) {
      res.data.info.create_date = wx.formatTime(new Date(res.data.info.create_date))
      this.setData({ info: res.data.info, recommend: res.data.recommend, collect: res.data.info.isCollect? 'like' : 'like-o', like: res.data.info.isLike? 'thumb-circle' : 'thumb-circle-o' })
    }
  },
  async docollect () {
    if (this.data.collect==='like-o') {
      const res = await wx.http.get('/user/userDataHandle',{
        type:1,
        article_id:this.data.id,
        action:'add'
      })
      console.log(res)
      if(res.errno===0){
        wx.toast('收藏成功')
        this.setData({collect:'like'})
      }
    }else{
      const res = await wx.http.get('/user/userDataHandle',{
        type:1,
        article_id:this.data.id,
        action:'del'
      })
      console.log(res)
      if(res.errno===0){
        wx.toast('取消收藏')
        this.setData({collect:'like-o'})
      }
    }
  },
  async dolike () {
    if (this.data.like==='thumb-circle-o') {
      const res = await wx.http.get('/user/userDataHandle',{
        type:2,
        article_id:this.data.id,
        action:'add'
      })
      console.log(res)
      if(res.errno===0){
        wx.toast('点赞成功')
        this.setData({like:'thumb-circle'})
      }
    }else{
      const res = await wx.http.get('/user/userDataHandle',{
        type:2,
        article_id:this.data.id,
        action:'del'
      })
      console.log(res)
      if(res.errno===0){
        wx.toast('取消点赞')
        this.setData({like:'thumb-circle-o'})
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})